package com.ajmd.onlineexam.service;

import com.ajmd.onlineexam.entity.DepartmentPO;
import com.ajmd.onlineexam.entity.OrganizationPO;
import com.ajmd.onlineexam.entity.SearchParam;
import com.ajmd.onlineexam.mapper.DepartmentMapper;
import com.ajmd.onlineexam.mapper.OrganizationMapper;
import com.ajmd.onlineexam.vo.DepartmentVO;
import com.ajmd.onlineexam.vo.OrganizationVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ajmd
 * @since 2023/5/1 19:08
 */
@Service
public class DepartmentService {
    @Resource
    private OrganizationMapper organizationMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private TeamService teamService;

    @Transactional
    public int addOrUpdate(DepartmentPO departmentPO){
        if(ObjectUtils.isNotEmpty(departmentPO.getId())){
            return departmentMapper.updateById(departmentPO);
        }
        //上级组织子部门数量+1
        LambdaUpdateWrapper<OrganizationPO> lambdaQueryWrapper = new LambdaUpdateWrapper<>();
        lambdaQueryWrapper.eq(OrganizationPO::getId, departmentPO.getOrganizationId()).setSql("department_num = department_num + 1");
        organizationMapper.update(null, lambdaQueryWrapper);
        departmentPO.setId(UUID.randomUUID().toString());
        return departmentMapper.insert(departmentPO);
    }

    public Page<DepartmentPO> getPage(Integer currentPage, Integer pageSize, SearchParam searchParams){
        LambdaQueryWrapper<DepartmentPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(searchParams.getSearchValue())){
            lambdaQueryWrapper.like(DepartmentPO::getDepartmentName,searchParams.getSearchValue());
        }
        Page<DepartmentPO> page = new Page<>(currentPage,pageSize);
        return departmentMapper.selectPage(page,lambdaQueryWrapper);
    }

    public List<OrganizationVO> getList(Integer userType){
        List<DepartmentPO> departmentPOS = departmentMapper.selectList(null);
        Map<String, List<DepartmentPO>> departmentPOsMap = departmentPOS.stream().collect(Collectors.groupingBy(DepartmentPO::getOrganizationId));
        //poMap转voMap
        Map<String, List<DepartmentVO>> departmentVOsMap = new HashMap<>();
        departmentPOsMap.forEach((organizationId,departmentPOList)->{
            List<DepartmentVO> departmentVOList = departmentPOList.stream().map(departmentPO->{
                DepartmentVO departmentVO = new DepartmentVO();
                BeanUtils.copyProperties(departmentPO,departmentVO);
                return  departmentVO;
            }).collect(Collectors.toList());
            departmentVOsMap.put(organizationId,departmentVOList);
        });

        List<OrganizationPO> organizationPOS = organizationMapper.selectList(null);
        List<OrganizationVO> organizationVOS = new ArrayList<>();
        for(OrganizationPO organizationPO:organizationPOS){
            OrganizationVO organizationVO = new OrganizationVO();
            BeanUtils.copyProperties(organizationPO,organizationVO);
            organizationVO.setChildren(departmentVOsMap.get(organizationPO.getId()));
            organizationVOS.add(organizationVO);
        }
        return organizationVOS;
    }

    public DepartmentPO getOne(String id){
        return departmentMapper.selectById(id);
    }

//    public int update(DepartmentPO departmentPO){
//        return departmentMapper.updateById(departmentPO);
//    }
    public int deleteByOrganizationId(String organizationId){
        LambdaQueryWrapper<DepartmentPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DepartmentPO::getOrganizationId,organizationId);
        return departmentMapper.delete(lambdaQueryWrapper);
    }
    public int delete(String id){
        DepartmentPO departmentPO = departmentMapper.selectById(id);
        //上级组织子部门数量-1
        LambdaUpdateWrapper<OrganizationPO> lambdaQueryWrapper = new LambdaUpdateWrapper<>();
        lambdaQueryWrapper.eq(OrganizationPO::getId, departmentPO.getOrganizationId()).setSql("department_num = department_num - 1");
        organizationMapper.update(null, lambdaQueryWrapper);
        teamService.deleteByDepartmentId(id);
        return departmentMapper.deleteById(departmentPO);
    }
}
